package cn.iocoder.devlop.module.annual.report.forinvestment.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.forinvestment.controller.vo.ForinvestmentCreateReqVO;
import cn.iocoder.devlop.module.annual.report.forinvestment.controller.vo.ForinvestmentOverwriteVO;
import cn.iocoder.devlop.module.annual.report.forinvestment.convert.ForinvestmentConvert;
import cn.iocoder.devlop.module.annual.report.forinvestment.dal.ForinvestmentMapper;
import cn.iocoder.devlop.module.annual.report.forinvestment.dal.dataobject.ForinvestmentDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业年报对外投资信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ForinvestmentServiceImpl implements ForinvestmentService {

    @Resource
    private BaseinfoService baseinfoService;

    @Resource
    private ForinvestmentMapper forinvestmentMapper;


    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;


    @Override
    public List<ForinvestmentDO> getForinvestmentList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return forinvestmentMapper.selectListByAncheid(ancheid);
    }


    @Override
    public void deleteForinvestmentAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<ForinvestmentDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(ForinvestmentDO::getAncheid,ancheid);
        forinvestmentMapper.delete(wrapperX);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteForinvestment(ForinvestmentOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<ForinvestmentDO> oldForinvestmentList = getForinvestmentList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<ForinvestmentDO> newForinvestmentList = updateReqVO.getForinvestmentList();
                List<ForinvestmentCreateReqVO> oldreqs = ForinvestmentConvert.INSTANCE.convertList3(oldForinvestmentList);
                List<ForinvestmentCreateReqVO> newreqs = ForinvestmentConvert.INSTANCE.convertList3(newForinvestmentList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newreqs,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteForinvestmentAll(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        createForinvestmentByList(updateReqVO.getRegno(),updateReqVO.getAncheyear(),updateReqVO.getForinvestmentList());
        baseinfoService.updateState(updateReqVO.getRegno(),updateReqVO.getAncheyear());
    }

    @Override
    public void createForinvestmentByList(String regno, String ancheyear, List<ForinvestmentDO> list) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(regno, ancheyear);
        if (null == baseinfo){
            baseinfoService.initBaseinfo(regno,ancheyear);
        }
        BaseinfoDO baseinfoDO = new BaseinfoDO();
        baseinfoDO.setForinvestsign("2");
        baseinfoDO.setAncheid(IdGenerateUtil.getAncheid(regno, ancheyear));
        if (CollUtil.isNotEmpty(list)) {
            for (ForinvestmentDO forinvestment : list) {
                forinvestment.setAncheid(ancheid);
                forinvestment.setOutinvid(IdUtil.simpleUUID());
                forinvestment.setEntclassify(entclassify);
            }
            baseinfoDO.setForinvestsign("1");
            forinvestmentMapper.insertBatch(list);
        }
        baseinfoService.baseinfoUpdate(baseinfoDO);
    }
}
